package com.hnqcgczy.controller;

import com.hnqcgczy.service.IOrderService;
import com.hnqcgczy.service.IProductService;
import com.hnqcgczy.service.IShopCartService;
import com.hnqcgczy.service.impl.OrderServiceImpl;
import com.hnqcgczy.service.impl.ProductServiceImpl;
import com.hnqcgczy.service.impl.ShopCartServiceImpl;
import com.hnqcgczy.vo.TOrder;
import com.hnqcgczy.vo.TOrderDetail;
import com.hnqcgczy.vo.TProduct;
import com.hnqcgczy.vo.TShopcart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/order")
public class OrderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        //登陆校验
        String tuId=(String)req.getSession().getAttribute("tuId");
        //判断如果tuId为null，跳转登陆
        if(tuId==null){
            resp.sendRedirect("/login.jsp");
            return;
        }
        //订单
        TOrder order=new TOrder();
        //订单编号  yyyyMMddHHmmss
        DateFormat df=new SimpleDateFormat("yyyyMMddhhmmss");
        String onumber=df.format(new Date());
        order.setToNumber(onumber);
        order.setTuId(tuId);
//        order.setToUserName(tuId);
        order.setToStatus(1);
        order.setToType(1);
        order.setToUserAddress("zhuzhou");
        order.setToCreateTime(new Timestamp(System.currentTimeMillis()));
        double tocost=0;

        List<TOrderDetail> orderDetails=new ArrayList<TOrderDetail>();
        //调用商品服务层获取商品信息
        IProductService productService=new ProductServiceImpl();
        IShopCartService cartService=new ShopCartServiceImpl();
        //获取商品编号
        String tpId=req.getParameter("tpId");
        String[] tpIds=tpId.split(",");

        for (String id : tpIds) {
            long count=1;
            TProduct product=productService.getProductById(Integer.parseInt(id));
            TShopcart cart=cartService.getShopCartByTuIdAndTpId(tuId,Integer.parseInt(id));

            //订单明细
            TOrderDetail detail=new TOrderDetail();
            detail.setToNumber(onumber);
            detail.setTpId(product.getTpId());
            detail.setTodCost(product.getTpPrice());
            orderDetails.add(detail);
            if(cart!=null){
                count=cart.getTsCount();
            }
            detail.setTodQuantity(count);
            tocost=tocost+product.getTpPrice()*count;
        }

        //订单总金额
        order.setToCost(tocost);
        //订单服务
        IOrderService orderService=new OrderServiceImpl();
        orderService.createOrder(order,orderDetails);

        //重定向到我的订单
        resp.sendRedirect("/myorder");
    }
}
